<?php
class Search_Model_Rewriteurl extends Apm_Model_Base{

	/**
	 * 都道府県CDの取得
	 *
	 * 都道府県CDの取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 都道府県CD
	 */
	public function getPrefCd($params)
	{
		$db_params = array();

		// 都道府県CD取得
		$sql = $this->generatePrefCdSql($params);
		$data = $this->dbGetData($sql,$db_params);

		return  $data['TODOUFUKEN_CD'];
	}

	/**
	 * 都道府県CD取得SQL文の取得
	 *
	 * 都道府県CD取得SQL文の取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 都道府県CD取得SQL
	 */
	public function generatePrefCdSql($params)
	{
		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M43_TODOUFUKEN_CD TODOUFUKEN_CD";
		$sql .= "    FROM";
		$sql .= "        M43TODOUFUKEN";
		$sql .= "    WHERE";
		$sql .= "        M43_TODOUFUKEN = '".$params['PrefNm']."'";

		return $sql;
	}

	/**
	 * 市区町村CDの取得
	 *
	 * 市区町村CDの取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 市区町村CD
	 */
	public function getCityCd($params)
	{
		$db_params = array();

		// 市区町村CD取得
		$sql = $this->generateCityCdSql($params);
		$data = $this->dbGetData($sql,$db_params);

		return  $data['SHIKUCHOUSON_CD'];
	}

	/**
	 * 市区町村CD取得SQL文の取得
	 *
	 * 市区町村CD取得SQL文の取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 市区町村CD取得SQL
	 */
	public function generateCityCdSql($params)
	{
		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M01_TODOUFUKEN_CD || M01_SHIKUCHOUSON_CD SHIKUCHOUSON_CD";
		$sql .= "    FROM";
		$sql .= "        M01JYUUSHO".$params['PrefCd'][0];
		$sql .= "    WHERE";
		$sql .= "        M01_TODOUFUKEN_CD = '".$params['PrefCd'][0]."'";
		$sql .= "        AND M01_SHIKUCHOUSON = '".$params['CityNm']."'";
		$sql .= "    GROUP BY";
		$sql .= "        M01_TODOUFUKEN_CD";
		$sql .= "        ,M01_SHIKUCHOUSON_CD";

		return $sql;
    }

	/**
	 * 町名CDの取得
	 *
	 * 町名CDの取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 町名CD
	 */
	public function getTownCd($params)
	{
		$db_params = array();

		// 町名CD取得
		$sql = $this->generateTownCdSql($params);
		$data = $this->dbGetData($sql,$db_params);

		return  $data['CHOUMEI_CD'];
	}

	/**
	 * 町名CD取得SQL文の取得
	 *
	 * 町名CD取得SQL文の取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 町名CD取得SQL
	 */
	public function generateTownCdSql($params)
	{
		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M01_TODOUFUKEN_CD || M01_SHIKUCHOUSON_CD || M01_CHOUMEI_CD CHOUMEI_CD";
		$sql .= "    FROM";
		$sql .= "        M01JYUUSHO".$params['PrefCd'][0];
		$sql .= "    WHERE";
		$sql .= "        M01_TODOUFUKEN_CD = '".$params['PrefCd'][0]."'";
		$sql .= "        AND M01_SHIKUCHOUSON = '".$params['CityNm']."'";
		$sql .= "        AND M01_CHOUMEI = '".$params['SectNm']."'";
		$sql .= "    GROUP BY";
		$sql .= "        M01_TODOUFUKEN_CD";
		$sql .= "        ,M01_SHIKUCHOUSON_CD";
		$sql .= "        ,M01_CHOUMEI_CD";

		return $sql;
    }
	/**
	 * 沿線CDの取得
	 *
	 * 沿線CDの取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 沿線CD
	 */
	public function getRailCd($params)
	{
		$db_params = array();

		// 沿線CD取得
		$sql = $this->generateRailCdSql($params);
		$data = $this->dbGetData($sql,$db_params);

		return  $data['ENSEN_CD'];
	}

	/**
	 * 沿線CD取得SQL文の取得
	 *
	 * 沿線CD取得SQL文の取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 沿線CD取得SQL
	 */
	public function generateRailCdSql($params)
	{
		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M02_ENSEN_CD ENSEN_CD";
		$sql .= "    FROM";
		$sql .= "        M02ENSEN";
		$sql .= "        ,M04ENSEN_AREA";
		$sql .= "    WHERE";
		$sql .= "        M02_ENSEN_CD = M04_ENSEN_CD";
		$sql .= "        AND M04_TODOUFUKEN_CD = '".$params['PrefCd'][0]."'";
		$sql .= "        AND M02_ENSEN_NAME = '".$params['RailNm']."'";

		return $sql;
	}

	/**
	 * 駅CDの取得
	 *
	 * 駅CDの取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 駅CD
	 */
	public function getStCd($params)
	{
		$db_params = array();

		// 駅CD取得
		$sql = $this->generateStCdSql($params);
		$data = $this->dbGetData($sql,$db_params);

		return  $data['EKI_CD'];
	}

	/**
	 * 駅CD取得SQL文の取得
	 *
	 * 駅CD取得SQL文の取得を行う
	 * @access	public
	 * @param	array
	 * @return	string 駅CD取得SQL
	 */
	public function generateStCdSql($params)
	{
		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M03_EKI_CD EKI_CD";
		$sql .= "    FROM";
		$sql .= "        M03EKI";
		$sql .= "    WHERE";
		$sql .= "        M03_ENSEN_CD = '".$params['RailCd'][0]."'";
		$sql .= "        AND M03_TODOUFUKEN_CD = '".$params['PrefCd'][0]."'";
		$sql .= "        AND M03_EKI_NAME = '".$params['StNm']."'";

		return $sql;
    }
}
?>